package fsm

import (
	"io/ioutil"
	"log"
	"myutil"

	"gopkg.in/yaml.v2"
)

type Status struct {
	Name string
	Next []string
}

type StatusList []Status

type FSMStatus struct {
	StatusType int `yaml:"statustype"`
	StatusList
}

type FSMStatusList []FSMStatus
type FSMConf struct {
	FSMStatusList
}

// ParseFile method , input is *file
func (fsmConf *FSMConf) ParseFile(confFile *string) error {
	buf, err := ioutil.ReadFile(*confFile)
	if err != nil {
		return err
	}
	err1 := yaml.Unmarshal(buf, fsmConf)
	if err1 != nil {
		log.Fatalf("error: %v", err1)
		return err1
	}

	return nil
}

// ParseBuf method input is []byte
func (fsmConf *FSMConf) ParseBuf(buf []byte) error {
	err := yaml.Unmarshal(buf, fsmConf)
	if err != nil {
		log.Fatalf("error Unmarshal : %v", err)
		return err
	}

	return nil
}

// Parser read the first file
func (fsmConf *FSMConf) Parser(fileName, delim string) error {
	output := myutil.Parser(fileName, delim)

	return fsmConf.ParseBuf(output)
}
